#include "my_led.h"
#include "eventos.h" // EventOS头文件
#include "eventos_def.h"
#include "evos_app.h"
/* data structure ----------------------------------------------------------- */
typedef struct eos_led_tag { // LED类
    eos_sm_t super;
    eos_u8_t status;
    // eos_bool_t status;
} eos_led_t;
static eos_led_t led; // led对象，单例模式
// 初始状态
static eos_ret_t state_init(eos_led_t *const me, eos_event_t const *const e);
// 上层状态
static eos_ret_t state_father(eos_led_t *const me, eos_event_t const *const e);
// 子状态
static eos_ret_t state_child(eos_led_t *const me, eos_event_t const *const e);

// Led的ON状态
static eos_ret_t state_on(eos_led_t *const me, eos_event_t const *const e);
// Led的Off状态
static eos_ret_t state_off(eos_led_t *const me, eos_event_t const *const e);
static void my_led_init(void)
{
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
    GPIO_InitTypeDef GPIO_InitStructure;
    GPIO_InitStructure.GPIO_Pin   = LED_PIN;
    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_Out_OD;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
    GPIO_Init(LED_PORT, &GPIO_InitStructure);
}

void eos_led_init(void)
{
    static eos_u32_t queue[32]; // 事件队列
    my_led_init();
    eos_sm_init(&led.super, 1, queue); // 状态机初始化
                                       // 状态机启动，以state_init作为初始状态。
    eos_sm_start(&led.super, EOS_STATE_CAST(state_init));
}

/* static state function ---------------------------------------------------- */
static eos_ret_t state_init(eos_led_t *const me, eos_event_t const *const e)
{
    // 订阅事件Event_Time_500ms
    // EOS_EVENT_SUB(Event_Time_500ms);
    EOS_EVENT_SUB(Event_1);
    EOS_EVENT_SUB(Event_2);
    EOS_EVENT_SUB(Event_1_1);

    // 使事件Event_Time_500ms，每隔500ms就被发送一次。
    eos_event_pub_period(Event_1_1, 500);
    me->status = 0;
    return EOS_TRAN(state_father);
}
static eos_ret_t state_father(eos_led_t *const me, eos_event_t const *const e)
{
    switch (e->topic) {
        case Event_Enter: // 状态state_father的进入事件
            return EOS_Ret_Handled;
        case Event_1:
            me->status = 1;
            return EOS_TRAN(state_child);
        case Event_2:
            me->status = 5;
            return EOS_TRAN(state_child);
        default:
            return EOS_SUPER(eos_state_top);
    }
}
static eos_ret_t state_child(eos_led_t *const me, eos_event_t const *const e)
{
    switch (e->topic) {
        case Event_Enter: // 状态state_father的进入事件
            me->status = 3;
            return EOS_Ret_Handled;
        case Event_1_1:
            me->status = 2;
            return EOS_Ret_Handled;
        default:
            return EOS_TRAN(state_father);
    }
}

static eos_ret_t state_on(eos_led_t *const me, eos_event_t const *const e)
{
    switch (e->topic) {
        case Event_Enter: // 状态state_on的进入事件
            LED_ON;
            return EOS_Ret_Handled;

        case Event_Time_500ms: // 收到Event_Time_500ms，跳转到state_off
            return EOS_TRAN(state_off);

        default:
            return EOS_SUPER(eos_state_top);
    }
}

static eos_ret_t state_off(eos_led_t *const me, eos_event_t const *const e)
{
    switch (e->topic) {
        case Event_Enter: // 状态state_on的进入事件
            LED_OFF;
            return EOS_Ret_Handled;
        case Event_Time_500ms: // 收到Event_Time_500ms，跳转到state_on
            return EOS_TRAN(state_on);
        default:
            return EOS_SUPER(eos_state_top);
    }
}